package com.dunqi.gpm.shunqing.home.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.home.adapter.ProjectTimelineAdapter;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.ProjectTimeline;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

public class ProjectSupervisoryManagementActivity extends BaseAct implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {

    private static final String FORSEND = "forSend";
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    private String LIST_TYPE;
    private ProjectTimelineAdapter adapter;
    List<ProjectTimeline> data = new ArrayList<>();

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryProgressListForSupervise(LIST_TYPE, page, true)
                .compose(RxSchedulers.<Page<ProjectTimeline>>io_main())
                .subscribe(new Consumer<Page<ProjectTimeline>>() {
                    @Override
                    public void accept(Page<ProjectTimeline> pages) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        ArrayList<ProjectTimeline> list = pages.getList();
                        if (pages.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
                        }
                        if (page == 1) {
                            adapter.replaceData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                        t("请求失败");
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {
        LIST_TYPE = FORSEND;
        SwipeUtil.init(swipeRefresh);
        adapter = new ProjectTimelineAdapter(this, data);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(this));
        adapter.setOnLoadMoreListener(this, recycler);
        swipeRefresh.setOnRefreshListener(this);

        adapter.setOnItemClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_supervisory_management;
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ProjectTimeline timeline = data.get(position);
//        long proPlanProgressId = timeline.getProPlanProgressId();
        SupervisoryManagementActivity.start(this, timeline);
    }
}
